package cn.wind.xboot.module.web.controller.sys;

import cn.wind.xboot.core.annotation.RestJsonController;
import cn.wind.xboot.core.res.ApiResult;
import cn.wind.xboot.logic.pf.entity.PfNotice;
import cn.wind.xboot.framework.domain.PageVo;
import cn.wind.xboot.framework.domain.SearchVo;
import cn.wind.xboot.framework.web.controller.BaseController;
import cn.wind.xboot.module.web.vo.pf.PfNoticeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * <p>Title: NoticeController</p>
 * <p>Description: TODO</p>
 *
 * @author xukk
 * @version 1.0
 * @date 2018/7/2
 */
@Slf4j
@RestJsonController
@Api(value = "notice",tags = "站点消息管理")
@RequestMapping("notice")
public class NoticeController extends BaseController<PfNotice, Long> {

    @Override
    public IService getService() {
        return pfNoticeService;
    }


    @GetMapping
    @ApiOperation(value = "分页获取站内消息")
    public ApiResult get(PfNoticeVo pfNoticeVo, @ModelAttribute PageVo pageVo, SearchVo searchVo) {
        QueryWrapper<PfNotice> ew=new QueryWrapper<PfNotice>();
        if(pfNoticeVo.getReadFlag()!=null){
            ew.lambda().eq(PfNotice::getReadFlag,pfNoticeVo.getReadFlag());
        }
        IPage<PfNotice> list =pfNoticeService.page(pageVo.init(),ew);
        return ApiResult.Custom().success().add(list);
    }

    @GetMapping("count")
    @ApiOperation(value = "统计站内消息")
    public ApiResult getCount() {
        Integer unreadCount=pfNoticeService.count(new QueryWrapper<PfNotice>().lambda().eq(PfNotice::getReadFlag,0));
        Integer readCount =pfNoticeService.count(new QueryWrapper<PfNotice>().lambda().eq(PfNotice::getReadFlag,1));
        Integer recycleCount=pfNoticeService.countByDeleteFlag(1);
        return ApiResult.Custom().success().put("unreadCount",unreadCount).put("readCount",readCount).put("recycleCount",recycleCount);
    }

    @GetMapping( "past")
    @ApiOperation(value = "分页获取已删除站内消息")
    public ApiResult getNoticeByPage(@ModelAttribute PageVo pageVo, Integer deleteFlag) {
        IPage<PfNotice> list =pfNoticeService.findByDeleteFlag(pageVo.init(), deleteFlag);
        return ApiResult.Custom().success().add(list);
    }

    @Override
    @DeleteMapping
    @ApiOperation(value = "批量通过ids删除")
    public ApiResult delete(@RequestParam Long[] ids) {
        pfNoticeService.removeByIds(Arrays.stream(ids).collect(Collectors.toList()));
        return ApiResult.Custom().success("批量通过id删除数据成功");
    }

    @PutMapping("read")
    @ApiOperation(value = "批量通过ids标记已读")
    public ApiResult read(Long[] ids) {
        pfNoticeService.updateReadFlagByIds(Arrays.stream(ids).collect(Collectors.toList()));
        return ApiResult.Custom().success("标记已读成功");
    }

    @PutMapping("resume")
    @ApiOperation(value = "批量通过ids标记已读")
    public ApiResult resume(@RequestParam Long[] ids) {
        pfNoticeService.resume(Arrays.stream(ids).collect(Collectors.toList()));
        return ApiResult.Custom().success("还原成功");
    }
}
